package com.bupt.kgplatform.config;


import com.bupt.kgplatform.Util.GetNowTime;
import com.bupt.kgplatform.Util.RedisCache;
import com.bupt.kgplatform.config.HandlerInterceptor.LoginHandlerInterceptor;
import com.bupt.kgplatform.config.HandlerInterceptor.RefreshTokenInterceptor;
import com.bupt.kgplatform.mapper.LogMapper;
import com.bupt.kgplatform.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class MyWebAppConfiguration implements WebMvcConfigurer {
//    @Value("${upload.file.location}")
//    private String fileLocation;
//    @Value("${upload.file.path}")
//    private String filePath;

    @Value("${image.save-path}")
    private String imageSavePath;
    /***图标映射路径*/
    @Value("${image.mapping-path}")
    private String imageMappingPath;

    @Autowired
    private RedisCache redisCache;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private GetNowTime getNowTime;

    @Autowired
    private LogMapper logMapper;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler(imageMappingPath + "**").addResourceLocations("file:" + imageSavePath);
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new RefreshTokenInterceptor(redisCache, userMapper, getNowTime, logMapper)).order(0);
        registry.addInterceptor(new LoginHandlerInterceptor())
                .excludePathPatterns(
                        "/user/login",
                        "/img/**"
                ).order(1);
    }
}
